package com.yinhe.music.yhmusic.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.SongClassifyAdapter;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceDialogFragment;
import com.yinhe.music.yhmusic.main.home.SongClassifySection;
import com.yinhe.music.yhmusic.widget.WrapContentGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class SongClassifyDialog extends BaseServiceDialogFragment
        implements BaseQuickAdapter.OnItemClickListener {

    private RecyclerView mRecyclerView;
    private SongClassifyAdapter songClassifyAdapter;

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    public static SongClassifyDialog newInstance() {
        SongClassifyDialog fragment = new SongClassifyDialog();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = (int) (window.getWindowManager().getDefaultDisplay().getHeight() * 0.5);
        window.setAttributes(params);
        getDialog().setCanceledOnTouchOutside(true); // 外部点击取消
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_song_classify, container);
        initView(view);
        initData();
        return view;
    }

    //初始化试图
    private void initView(View view) {
        mRecyclerView = view.findViewById(R.id.recycler_view);
    }

    //初始化数据
    private void initData() {
        List<SongClassifySection> data = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            data.add(new SongClassifySection(true,"语言" + i));
            for (int j = 0; j < 7; j++) {
                data.add(new SongClassifySection("标签" + j));
            }
        }

        songClassifyAdapter = new SongClassifyAdapter(data);
        songClassifyAdapter.setOnItemClickListener(this);
        mRecyclerView.setLayoutManager(new WrapContentGridLayoutManager(getActivity(), 3,
                GridLayoutManager.VERTICAL, false));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(songClassifyAdapter);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        showToast(songClassifyAdapter.getData().get(position).t);
    }
}
